package com.xunqi.sell.service.impl;

import com.xunqi.sell.dto.CartDTO;
import com.xunqi.sell.enums.ProductStatusEnum;
import com.xunqi.sell.enums.ResultEnum;
import com.xunqi.sell.exception.SellException;
import com.xunqi.sell.pojo.ProductInfo;
import com.xunqi.sell.repository.ProductInfoRepository;
import com.xunqi.sell.service.ProductService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@CacheConfig(cacheNames = "product")
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductInfoRepository productInfoRepository;

    @Override
    @Cacheable(key = "123")
    public ProductInfo findOne(String productId) {
        return productInfoRepository.findAllById(productId);
    }


    @Override
    public List<ProductInfo> findUpAll() {
        return productInfoRepository.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    @CachePut(value = "findAll",keyGenerator = "keyGenerator")
    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        return productInfoRepository.findAll(pageable);
    }

    @CachePut(key = "123")
    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return productInfoRepository.save(productInfo);
    }

    @Override
    @Transactional
    public void increaseStock(List<CartDTO> CartDTOList) {
        for (CartDTO cartDTO : CartDTOList) {
            ProductInfo productInfo = productInfoRepository.findAllById(cartDTO.getProductId());
            if (productInfo == null) {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer result = productInfo.getProductStock() + cartDTO.getProductQuantity();
            //更改库存
            productInfo.setProductStock(result);
            productInfoRepository.save(productInfo);
        }
    }

    @Override
    @Transactional
    public void decreaseStock(List<CartDTO> CartDTOList) {
       for (CartDTO CartDTO : CartDTOList) {
           ProductInfo productInfo = productInfoRepository.findAllById(CartDTO.getProductId());
           if (productInfo == null) {
               throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
           }
           //减库存
           Integer result = productInfo.getProductStock() - CartDTO.getProductQuantity();

           if (result < 0) {
               throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
           }
           productInfo.setProductStock(result);

           productInfoRepository.save(productInfo);
       }
    }

    @Override
    public ProductInfo onSale(String productId) {

        ProductInfo productInfo = productInfoRepository.findAllById(productId);

        if (productInfo == null) {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if (productInfo.getProductStatus().equals(ProductStatusEnum.UP.getCode())) {
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }

        //更新操作
        productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
        return productInfoRepository.save(productInfo);
    }

    @Override
    public ProductInfo offSale(String productId) {
        ProductInfo productInfo = productInfoRepository.findAllById(productId);

        if (productInfo == null) {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if (productInfo.getProductStatus().equals(ProductStatusEnum.DOWN.getCode())) {
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }

        //更新操作
        productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
        return productInfoRepository.save(productInfo);
    }
}
